package domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * This class is responsible for remembering the email & the root directory. <br />
 * It can load & save these settings to a file.
 *
 * @author ParaSait
 */
public class Settings {

    private static final String FILELOCATION = "settings.cfg";
    private File file;
    private String email;
    private String rootDirectory;

    /**
     * Default constructor. <br />
     * Automatically loads the settings from the file.
     */
    public Settings() {
        setEmail("");
        setRootDirectory("");
        loadSettings();
    }

    /**
     * Load the settings from the settings file. <br />
     * This method is called from the default constructor.
     */
    private void loadSettings() {
        try {
            file = new File(FILELOCATION);
            if (!file.exists()) {
                file.createNewFile();
            }

            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            int i = 0;
            while ((line = reader.readLine()) != null) {
                if (i == 0) {
                    setEmail(line);
                } else if (i == 1) {
                    setRootDirectory(line);
                }
                i++;
            }
            reader.close();
        } catch (IOException ioe) {
            // Can't load settings file
            // No need to throw it because it won't give problems:
            // User will just have to manually fill in email & rootdirectory
        }
    }

    /**
     * Persist the current settings (email, rootDirectory) to the settings file.
     */
    public void saveSettings() {
        try {
            file = new File(FILELOCATION);
            if (!file.exists()) {
                file.createNewFile();
            }

            PrintWriter writer = new PrintWriter(new FileWriter(file));
            writer.write(getEmail() + "\n");
            writer.write(getRootDirectory() + "\n");
            writer.close();
        } catch (IOException ioe) {
            // Can't save to settings file
            // No need to throw it because it won't give problems:
            // User will just have to manually fill in email & rootdirectory on next start
        }
    }

    /**
     * Get the email out of the settings file. <br />
     * This way you don't need to re-type your email to sign in.
     *
     * @return email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets the email of the user that logged in. <br />
     * Settings are not persisted until saveSettings() is called!
     *
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Get the location of the synchronize folder out of the settings file. <br />
     * This way you don't need to re-set the synchronize folder every time you sign in.
     *
     * @return rootDirectory
     */
    public String getRootDirectory() {
        return rootDirectory;
    }

    /**
     * Sets the location of the synchronize folder. <br />
     * Settings are not persisted until saveSettings() is called!
     *
     * @param rootDirectory
     */
    public void setRootDirectory(String rootDirectory) {
        this.rootDirectory = rootDirectory;
    }

}
